$(function () {
    $("#mytable").bootstrapTable({
        url:"/rbac/role/group/pages",   //请求地址
        striped : true, //是否显示行间隔色
        pageNumber : 1, //初始化加载第一页
        pagination : true,//是否分页
        sidePagination : 'server',//server:服务器端分页|client：前端分页
        dataField:"list",
        ajaxOptions:{
            headers: {"Authorization": localStorage.getItem('token')}
        },
        maintainSelected: true,
        pageSize : 20,//单页记录数
        pageList : [10, 20,50],//可选择单页记录数
        showRefresh : false,//刷新按钮
        queryParams : function(params) {//上传服务器的参数
            var temp = {
                pageSize : params.limit, // 每页显示数量
                startRow : params.offset, // SQL语句起始索引
                pageNum: (params.offset / params.limit) + 1,   //当前页码
                   roleGroup:$('#search_roleGroup').val(),
                   roleGroupCode:$('#search_roleGroupCode').val(),
                   remark:$('#search_remark').val(),
            };
            return temp;
        },columns : [
            {
            field : 'checked',
            checkbox: true,
            align: 'center',
            valign: 'middle',
        },
        {
            title : '角色组ID',
            field : 'id',
            sortable : true
        },
        {
            title : '角色组名',
            field : 'roleGroup',
            sortable : true
        },
        {
            title : '角色组编码',
            field : 'roleGroupCode',
            sortable : true
        },
        {
            title : '备注信息',
            field : 'remark',
            sortable : true
        },
        {
            title : '创建时间',
            field : 'createTime',
            sortable : true
        },
        {
            title : '修改时间',
            field : 'updateTime',
            sortable : true
        },
        {
            title : '停用状态:',
            field : 'stopStatus',
            sortable : true,
            formatter:function (value){
                if(value=='0'){
                    return '启用';
                }
                return '停用';
            }
        },

            {
                title: '操作',
                field: 'id',
                align: 'center',
                formatter: function (value, row, index) {
                    var de = '<button href="#" id="detail" onclick="detail('+row.id+')" class="btn btn-primary" data-toggle="modal" data-target="#editModal" >详情</button> '
                    var e = '<button href="#" id="edit" onclick="editModal('+row.id+')" class="btn btn-warning"  data-toggle="modal" data-target="#editModal">编辑</button> '
                    var d = '<button href="#" id="delete" onclick="deleteMs('+row.id+')" class="btn btn-danger" style="margin-left:10px;">删除</button> ';
                    return de + e + d;
                }
            }]
    })
})
$('#search_btn').click(function() {
    $('#mytable').bootstrapTable('refresh', {
        url : '/rbac/role/group/pages',
        ajaxOptions:{
            headers: {"Authorization": localStorage.getItem('token')}
        },
    });
})
function detail(id){
    getMsg(id,'detail');

}

function getMsg(id,flag){
    $.ajax({
        type: 'post',
        sync: false,
        url: '/rbac/role/group/get',
        data: "id="+id,
        beforeSend: function (XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader("Authorization", localStorage.getItem("token"));
        },
        success: function (data) {
            if (data.code==200) {
                var row=data.data;
                $('#mytable').bootstrapTable('refresh');
                $('#id').val(row.id);
                $('#roleGroup').val(row.roleGroup);
                $('#roleGroupCode').val(row.roleGroupCode);
                $('#remark').val(row.remark);
                $('#createTime').val(row.createTime);
                $('#updateTime').val(row.updateTime);
                $('#stopStatus').val(row.stopStatus);
                $('#hidden').val(row.hidden);
                if(flag=='detail'){
                    $('#closeBtn').hide();
                    $('#modifyBtn').hide();
                    $('#myModalLabel').text("详情");
                    $('#modifyBtn').text("确定");
                }else{
                    $('#closeBtn').show();
                    $('#modifyBtn').show();
                    $('#myModalLabel').text("编辑");
                    $('#modifyBtn').text("确定");
                }
            }
            else {
                alert('修改失败');
            }
        }
    })
}

function editModal(id){
   getMsg(id,'');
}
function addObject(){
    $('#myModalLabel').text("添加");
    $('#modifyBtn').text("确定");
    $('#id').val('');
    $('#roleGroup').val('');
    $('#roleGroupCode').val('');
    $('#remark').val('');
    $('#createTime').val('');
    $('#updateTime').val('');
    $('#stopStatus').val('');
    $('#hidden').val('');
    $('#closeBtn').show();
    $('#modifyBtn').show();
}

function batchDel(){
    let rows= $('#mytable').bootstrapTable('getSelections');
    if (rows.length == 0) {// rows 主要是为了判断是否选中，下面的else内容才是主要
        alert("请先选择要删除的记录!");
        return;
    } else {
        let ids = new Array();// 声明一个数组
        $(rows).each(function() {// 通过获得别选中的来进行遍历
            ids.push(this.id);// cid为获得到的整条数据中的一列
        });
        deleteMs(ids)
    }
}

function editInfo() {
    var data={
            id: $('#id').val(),
            roleGroup: $('#roleGroup').val(),
            roleGroupCode: $('#roleGroupCode').val(),
            remark: $('#remark').val(),
            createTime: $('#createTime').val(),
            updateTime: $('#updateTime').val(),
            stopStatus: $('#stopStatus').val(),
            hidden: $('#hidden').val(),
    }
    $.ajax({
        type: 'post',
        url: '/rbac/role/group/add',
        contentType: "application/json",
        dataType: "json",
        beforeSend: function (XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader("Authorization", localStorage.getItem("token"));
        },
        data: JSON.stringify(data),
        success: function (data) {
            if (data) {
                $('#mytable').bootstrapTable('refresh');
                $('#editModal').modal('hide');
            }
            else {
                alert('修改失败');
            }
        }
    })
}


function deleteMs(ids) {
    $.ajax({
        url : "/rbac/role/group/del",
        sync:false,
        data : "ids=" + ids,
        beforeSend: function (XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader("Authorization", localStorage.getItem("token"));
        },
        type : "post",
        dataType : "json",
        success : function(data) {
            $('#mytable').bootstrapTable('refresh', {
                url : '/rbac/role/group/pages'
            });
        }
    });
}


function downloadExcel(){
    window.location.href='/rbac/role/group/download?token='+localStorage.getItem("token");
}

function importExcel(){
    $("#fileImport").click();
}

function findFile(){
    var formData = new FormData();
    formData.append("file", $("#fileImport")[0].files[0]);
    $.ajax({
        url: '/rbac/role/group/upload', /*接口域名地址*/
        type: 'post',
        data: formData,
        beforeSend: function (XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader("Authorization", localStorage.getItem("token"));
        },
        contentType: false,
        processData: false,
        success: function (res) {
            console.log(res.data);
            if (res.code=='200') {
                alert(res.message);
            } else {
                console.log("上传文件失败");
            }
        }
    })
}

function exportExcel(){
    let rows= $('#mytable').bootstrapTable('getSelections');
    if (rows.length == 0) {// rows 主要是为了判断是否选中，下面的else内容才是主要
        alert("请先选择要导出的记录!");
        return;
    } else {
        let ids = new Array();// 声明一个数组
        $(rows).each(function() {// 通过获得别选中的来进行遍历
            ids.push(this.id);// cid为获得到的整条数据中的一列
        });
        window.location.href='/rbac/role/group/download?ids='+ids+"&token="+localStorage.getItem("token");
    }
}